require "Common/define"
require "Logic/PetManager"
require "Net/send_Msg"
require "Logic/Config"
require "proto/item/message/ReqUseItemMessage"
require "proto/pet/message/ReqSelfMatingMessage"
require "proto/pet/message/ReqAskMatingMessage"
require "proto/pet/message/ReqRefuseMatingMessage"
require "proto/pet/message/ReqAgreeMatingMessage"
require "proto/pet/message/ReqCancelMatingMessage"
require "proto/pet/message/ReqMergePetMessage"
require "proto/pet/message/ReqLockPetMessage"
require "proto/pet/message/ReqUnlockPetMessage"
require "proto/pet/message/ReqPetFollowMessage"
require "proto/pet/message/ReqCancelPetFollowMessage"
require "proto/pet/message/ReqGetFirstPetMessage"
require "proto/pet/message/ReqPetTryPartUpdateMessage"
require "proto/pet/message/ReqAllPetMessage"
require "proto/pet/message/ReqPetPartListInfoMessage"
require "proto/pet/message/ReqPetPartUpLvMessage"
require "proto/pet/message/ReqPetPartResetLvMessage"
PetHandler = PetHandler or {}
local this = PetHandler
G.serverData = G.serverData or {}


-- 接收到宠物的初始化信息
function this.ResPets(msg)
    if msg.state == 0 then
        PetManager.ResPets(msg)
    end
end
function this.ReqGetFirstPet()
    local _req = ReqGetFirstPetMessage.New()
    sendReqMessage(_req)
end
function this.ResGetFirstPet(message)
    if message.state == 0 then
        PetManager.ResGetFirstPet(message)
    end
end

-- 宠物锁定 Start
function this.ReqLockPet(petId)
    local req = ReqLockPetMessage.New()
    req["petId"] = petId
    sendReqMessage(req)
end
function this.ResLockPet(msg)
    if msg.state == 0 then
        PetManager.ResLockPet(msg.petId, true)
    end
end
function this.ReqUnlockPet(petId)
    local req = ReqUnlockPetMessage.New()
    req["petId"] = petId
    sendReqMessage(req)
end
function this.ResUnlockPet(msg)
    if msg.state == 0 then
        PetManager.ResLockPet(msg.petId, false)
    end
end
-- 宠物锁定 End

--宠物跟随 Start
function this.ReqPetFollow(petId)
    local req = ReqPetFollowMessage.New()
    req["petId"] = petId
    sendReqMessage(req)
end
function this.ResPetFollow(msg)
    if msg.state == 0 then
        PetManager.ResPetFollow(msg.petId, true)
    end
end
function this.ReqCancelPetFollow(petId)
    local req = ReqCancelPetFollowMessage.New()
    req["petId"] = petId
    sendReqMessage(req)
end
function this.ResCancelPetFollow(msg)
    if msg.state == 0 then
        PetManager.ResPetFollow(msg.petId, false)
    end
end
function this.ResAddPet(msg)
    if msg.state == 0 then
        PetManager.ResAddPet(msg)
    end
end

--宠物跟随 End

--融合 Start
function this.ReqMergePet(petIds)
    local req = ReqMergePetMessage.New()
    req["petIds"] = petIds
    sendReqMessage(req)
end
function this.ResMergePet(msg)
    if msg.state == 0 then
        PetManager.ResMergePet(msg)
    end
end

--融合 End

-- 拟变 Start
function this.ReqSelfMating(templatePetId, mutatePetId,lockPart)
    local req = ReqSelfMatingMessage.New()
    req["tmplPetId"] = templatePetId
    req["mutatePetId"] = mutatePetId
    req["lockParts"] = lockPart
    sendReqMessage(req)
end
function this.ResSelfMating(msg)
    if msg.state == 0 then
        PetManager.ResSelfMating(msg)
    end
end

function this.ReqCancelMating(templatePetId, mutatePetId)
    local req = ReqCancelMatingMessage.New()
    req["tmplPetId"] = templatePetId
    req["mutatePetId"] = mutatePetId
    sendReqMessage(req)
end
function this.ResCancelMating(message)
    if message.state == 0 then
        PetManager.ResCancelMating(message)
    end
end
function this.ResMatingSuccess(message)
    if message.state == 0 then
        PetManager.ResMatingSuccess(message)
    end
end
function this.ResMatingFailed(message)
    if message.state == 0 then
        PetManager.ResMatingFailed(message)
    end
end
function this.ReqAskMating(askPlayerId, templatePetId, mutatePetId, message,lockParts)
    local req = ReqAskMatingMessage.New()
    req["askPlayerId"] = askPlayerId
    req["tmplPetId"] = templatePetId
    req["mutatePetId"] = mutatePetId
    req["message"] = message
    req["lockParts"] = lockParts;
    sendReqMessage(req)
end
function this.ResAskMating(message)
    if message.state == 0 then
        PetManager.ResAskMating(message)
    end
end
-- 拟变 End

-- 申请 Start
function this.ReqRefuseMating(templatePetId, mutatePetId)
    local req = ReqRefuseMatingMessage.New()
    req["tmplPetId"] = templatePetId
    req["mutatePetId"] = mutatePetId
    sendReqMessage(req)
end
function this.ResRefuseMating(msg)
    if msg.state == 0 then
        PetManager.ResRefuseMating(msg)
    end
end

function this.ReqAgreeMating(templatePetId, mutatePetId)
    local req = ReqAgreeMatingMessage.New()
    req["tmplPetId"] = templatePetId
    req["mutatePetId"] = mutatePetId
    sendReqMessage(req)
end
function this.ResAgreeMating(message)
    if message.state == 0 then
        PetManager.ResAgreeMating(message)
    end
end
function this.ResApplyMating(message)
    if message.state == 0 then
        PetManager.ResApplyMating(message)
        FireEvent(Config.EventType.FresPetPage)
    end
end
-- 申请 End

-- 申请宠物试穿保存 start

function this.ReqPetTryPartUpdate(id, genes)
    local req = ReqPetTryPartUpdateMessage.New()
    req["tryId"] = id
    req["genes"] = genes
    sendReqMessage(req)
end
function this.ResPetTryPartUpdateHandler(message)
    if message.state == 0 then
        FireEvent(Config.EventType.SaveTryPart);
    end
end

function this.PetTryPartListHandler(message)
    if message.state == 0 then
        for k, v in pairs(message.infos) do
            PetManager.setSaveTryPartDataGroup(v.id, v.genes);
        end
    end
end

function this.ReqAllPet()
    local req = ReqAllPetMessage.New()
    sendReqMessage(req)
end
--飞飞魂印
function this.ReqPartListInfo()
    local req = ReqPetPartListInfoMessage.New()
    sendReqMessage(req)
end

function this.ResPartListInfo(msg)
    PetManager.PartListInfo(msg)
    FireEvent(Config.EventType.PetPartsAttrUpdate)
end

function this.ReqUpLv(partId,lv)
    local req = ReqPetPartUpLvMessage.New()
    req.partId = partId
    req.partCurrLv = lv
    sendReqMessage(req)
end

function this.ResUpLv(msg)
    PetManager.UpdateOnePart(msg)
    FireEvent(Config.EventType.PetPartAttrUpLv,msg)
end

function this.ReqPartReset(partId)
    local req = ReqPetPartResetLvMessage.New()
    req.partId = partId and partId or -1
    sendReqMessage(req)
end

function this.ResPartReset(msg)
    PetManager.PartListInfo({})
    PetManager.RestBackItems(msg.resItems)
    FireEvent(Config.EventType.PetPartsAttrUpdate)
end
